package com.zhiyou100.video.webaction.course;

import java.security.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhiyou100.video.model.Course;
import com.zhiyou100.video.model.Speaker;
import com.zhiyou100.video.model.Subject;
import com.zhiyou100.video.model.Video;
import com.zhiyou100.video.service.CourseService;
import com.zhiyou100.video.utils.Page;

@SuppressWarnings("all")
@Controller("CourseAction")
@Scope(scopeName="prototype")
public class CourseAction extends ActionSupport implements ModelDriven<Course>{
	@Autowired
	CourseService cs;
	private Course course = new Course();
	@Override
	public Course getModel() {
		return course;
	}
	private Integer page;
	private Integer subid;
	
	public Integer getSubid() {
		return subid;
	}

	public void setSubid(Integer subid) {
		this.subid = subid;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public String courselist() throws Exception {
		Integer currentpage= page == null ? 1 :page;
		Page<Course> pagep = cs.selectCourse(currentpage);
		ActionContext.getContext().put("pagep", pagep);
		return SUCCESS;
	}
	public String courseadd() throws Exception {
		
		List<Subject> list =cs.selectSubject();
		ActionContext.getContext().put("Subject", list);
		return "courseadd";
	}
	public String addcourse() throws Exception {
		Subject subject = new Subject();
		subject.setId(subid);
		course.setSubjectId(subject);
		course.setInsertTime(new Date(System.currentTimeMillis()));
		course.setUpdateTime(new Date(System.currentTimeMillis()));
		cs.addCourse(course);
		return "courselist";
	}
	public String coursemanage() throws Exception {
		List<Subject> list =cs.selectSubject();
		Course cour= cs.selectCourseId(course.getId());
		ActionContext.getContext().put("subject", list);
		ActionContext.getContext().put("list", cour);
		
		return "coursemanage";
	}
	public String statistics() throws Exception {
		List<Object[]> list = cs.selectVideo();
		
		StringBuffer buffer = new StringBuffer();
		
		
		int a = 0;
		buffer.append("[");
		for (Object[] obj : list) {
			 
			buffer.append(obj[0]);
			a=a+1;
			if(a<list.size()){
			buffer.append(",");	
			}
		}
		buffer.append("]");
		StringBuffer bu = new StringBuffer();
		int b = 0;
		bu.append("['");
		for (Object[] obj : list) {
			bu.append(obj[1]);
			b=b+1;
			if(b<list.size()){
				bu.append("','");	
			}
		}
		bu.append("']");
		ActionContext.getContext().put("name", bu);
		ActionContext.getContext().put("count", buffer);
		return "statistics";
	}
	
}